package cn.org.timespace.ons.inside.mina.filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.ons.inside.mina.util.ProcessInPackage;
import cn.org.timespace.ons.util.AryUtil;

public class ESInProtocalPack {
	private final static Log log = LogFactory.getLog(ESInProtocalPack.class);
	public final static String SYNC_FRAME_NAME_UP = "ZJX";	//同步帧(上传)
	public final static String SYNC_FRAME_NAME_DOWN = "JXT";//同步帧(下传)
	
	private String syncFrameSort = "up";//同步帧类型（up：ZJX，down：JXT）,默认为up
	
	private String syncFrame;//同步帧，长度：3
	private String machineNo;//机器号，长度：4
	private String packageLength;//包长度，长度：4(即功能号+包内容，2＋N：ASCII表示的16进制数，例 2+N=10  L=000A)
	private String functionNo;//功能号，长度：2
	private String packageContents;//包内容，长度：N
	private String checkCode;//较验和，长度：2

	public String toString(){
	    StringBuffer sb = new StringBuffer();
	    sb.append(syncFrame);
	    sb.append(machineNo);
	    sb.append(packageLength);
	    sb.append(functionNo);
	    sb.append(packageContents);
	    sb.append(checkCode);
	    return sb.toString();
	}

	/**
	 * 取得包长度(10进制)
	 * @return
	 */
	public int getPackageLengthInt() {
		int length = -1;
		try {
			length = AryUtil.hexToInt(packageLength);
		}
		catch(Exception e) {
			//数据类型转换错误
			log.error("数据类型转换错误" + e);
			e.printStackTrace();
		}
		return length;
	}
	
	/**
	 * 计算包长度，用于回复
	 * @return
	 */
	public String calPackageLength() {
		
		int pcl = 0;
		if(packageContents != null) {
			pcl = packageContents.length();
		}
		
		String pl = AryUtil.decimalToHex(2 + pcl);
		//包长度字符串不足四位，在前面加0
		pl = ProcessInPackage.StrAddLength(4, pl);
		return pl.toUpperCase();
	}
	
	/**
	 * 检查校验和是否正确
	 * @param pack
	 * @return
	 */
	public boolean verifyCheckCode() {
		
		String code = calCheckCode();
		
		//比较校验和
		if(!code.equalsIgnoreCase(checkCode)) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * 计算校验和，用于回复
	 * @return
	 */
	public String calCheckCode() {
		String contents = functionNo + packageContents;//内容部分(功能号+包内容）
		
		//计算校验和
		contents = AryUtil.string2HexAdd(contents);
		String code = contents.substring(contents.length() - 2, contents.length());//取最后两位,算出校验和
		
		return code.toUpperCase();
	}
	
	/**
	 * 自动生成校验和和包长度，用于回复
	 */
	public void setCheckCodeAndPackageLength() {
		setCheckCode(calCheckCode());
		setPackageLength(calPackageLength());
	}
	
	public void reset() {
		this.setSyncFrame("");
		this.setMachineNo("");
		this.setPackageLength("");
		this.setFunctionNo("");
		this.setPackageContents("");
		this.setCheckCode("");
	}

	public String getSyncFrame() {
		return syncFrame;
	}

	public void setSyncFrame(String syncFrame) {
		this.syncFrame = syncFrame;
	}

	public String getMachineNo() {
		return machineNo;
	}

	public void setMachineNo(String machineNo) {
		this.machineNo = machineNo;
	}

	public String getPackageLength() {
		return packageLength;
	}

	public void setPackageLength(String packageLength) {
		this.packageLength = packageLength;
	}

	public String getPackageContents() {
		return packageContents;
	}

	public void setPackageContents(String packageContents) {
		this.packageContents = packageContents;
	}

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	public void setFunctionNo(String functionNo) {
		this.functionNo = functionNo;
	}

	public String getFunctionNo() {
		return functionNo;
	}

	public String getSyncFrameSort() {
		return syncFrameSort;
	}

	public void setSyncFrameSort(String syncFrameSort) {
		this.syncFrameSort = syncFrameSort;
	}

}
